<?php

class ColumnService extends \BaseController {

	//把栏目名存入column
	public static function add($column_name, $table_name){
		//把栏目名存入栏目表
		$column = new Column;
		$column->column_name = $column_name;
		$column->table_name = $table_name.'s';
		$column->save();
		return $column->id;
	}

	//栏目新增字段
	public static function fild($column_id,  $field_name, $display_name, $field_type){
		$field = new Field;
		$field->column_id = $column_id;
		$field->field_name = $field_name;
		$field->display_name = $display_name;
		$field->field_type = $field_type;
		$field->save();
		$column_name=Column::where('id','=',$column_id)->first()->column_name;
		Schema::table($column_name, function($table)
		{
		    $table->$field_type($field_name);
		});
	}

	public static function create($table_name, $fields, $field_type, $insertId, $display_name)
	{
		//创建新增栏目表columns
		Schema::create($table_name.'s', function($table) use ($fields, $field_type, $insertId, $display_name){
			$table->increments('id');
			for($i=0; $i<count($fields); $i++){
				$table->$field_type[$i]($fields[$i]);
			}
			$table->timestamps();
		});
		//创建model
		ModelService::store($table_name);
		
		//把新栏目字段存入字段表fields
		for($i=0;$i<count($fields); $i++){
			$field = new Field;
			$field->field_name = $fields[$i];
			$field->display_name = $display_name[$i];
			$field->field_type = $field_type[$i];
			$field->column_id=$insertId;
			$field->save();
		}
	}

	/**
	 * Update the specified resource in storage.
	 * PUT /service/columnservice/{id}
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public static function update($id)
	{
		//
	}

}